<?php

class AdminLoginURL extends Extension implements i18nEntityProvider {
    function onBeforeSecurityLogin() {
    	$controller = Controller::curr();
		if($controller instanceof CMSSecurity){
			return;
		}
		
		if(Session::get('MemberLoginForm.force_message')){
			Session::set('MemberLoginForm.force_message', 0);
			if($controller instanceof Dashboard || $controller instanceof DatabaseAdmin || $controller instanceof DevelopmentAdmin || $controller instanceof LeftAndMain || preg_match('/\/admin/i', $controller->request->getVar('BackURL')) || preg_match('/\/dev/i', $controller->request->getVar('BackURL'))){
        		Session::set('MyAdminForm.force_message', 1);
			}
			else{
				Session::set('MyLoginForm.force_message', 1);
			}
		}
		
		$force_message = $this->owner->LoginForm()->Message();
		$this->owner->LoginForm()->clearMessage();

    	if($controller instanceof Dashboard || $controller instanceof DatabaseAdmin || $controller instanceof DevelopmentAdmin || $controller instanceof LeftAndMain || preg_match('/\/admin/i', $controller->request->getVar('BackURL')) || preg_match('/\/dev/i', $controller->request->getVar('BackURL'))){
    		Session::set('AdminLogin.force_message', $force_message);
			return $controller->redirect(Controller::join_links('administrator', 'login'));
		}
		else{
			Session::set('MemberLogin.force_message', $force_message);
			return $controller->redirect(Controller::join_links('member', 'login'));
		}
	}

	function provideI18nEntities() {
		$entities = array();
		$entities['CMSMain.ACCESSALLINTERFACES'] = array('Access to all backend admin sections');
		$entities['LeftAndMain.PERMAGAIN'] = array('You have been logged out of the admin backend.  If you would like to log in again, enter a username and password below.');
		$entities['LeftAndMain.PERMALREADY'] = array("I'm sorry, but you can't access that part of the admin backend.  If you want to log in as someone else, do so below");
		$entities['LeftAndMain.PERMDEFAULT'] = array('Please choose an authentication method and enter your credentials to access the admin backend.');
		$entities['Permission.CMS_ACCESS_CATEGORY'] = array('Backend Admin Access');
		return $entities;
	}
}
